package com.by.controller;

import com.by.bean.Dict;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.group.V;
import com.by.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "字典管理")
@RequestMapping("/api")
@CrossOrigin
public class DictSController extends BaseController {

    @Autowired
    DictService dictService;

    @ApiOperation("保存字典")
    @PostMapping("/dict")
    public ResultBean save(@Validated @RequestBody Dict dict, BindingResult result) {
        V.validate(result);
        int rs = dictService.save(dict);
        return saveResult(rs);
    }

    @ApiOperation("修改字典")
    @PutMapping("/dict")
    public ResultBean update(@Validated @RequestBody Dict dict, BindingResult result) {
        V.validate(result);
        int rs = dictService.update(dict);
        return saveResult(rs);
    }


    @ApiOperation("根据pid查询字典")
    @GetMapping("/dict/child/{pid}")
    public ResultBean<List<Dict>> query(@PathVariable("pid") int pid) {
        List list = dictService.query(pid);
        return success("字典列表", list);
    }
}
